package com.fenghongzhang.day008view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class FengText extends View {
    String string;
    int color;
    int width;
    int height;
    float size;
    private static final String TAG = "FengText";
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    //new
    public FengText(Context context) {
        super(context);
    }

    //xml
    public FengText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //加载参数文件,给xml的数据赋值.
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FengText);
        string = typedArray.getString(R.styleable.FengText_wenzi);
        color = typedArray.getColor(R.styleable.FengText_color, Color.BLUE);
        size = typedArray.getDimension(R.styleable.FengText_size, 20);

        //释放
        typedArray.recycle();
    }




    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        MeasureSpec.getSize()

        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        //三种
        switch (mode){
            case MeasureSpec.EXACTLY: //100dp  match_parent
                Log.i(TAG, "onMeasure: 精准模式"+size);
                width = size;
                break;
            case MeasureSpec.AT_MOST: //wrap_content
                Log.i(TAG, "onMeasure: 最大值模式"+size);
                //取到当前view的内容的大小.把大小重新赋值即可.

                width = 100;
                break;
            case MeasureSpec.UNSPECIFIED: //不限制view的最大值
                Log.i(TAG, "onMeasure: 自由模式");
                break;
        }


        int heightModel = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //三种
        switch (heightModel){
            case MeasureSpec.EXACTLY: //100dp  match_parent
                Log.i(TAG, "onMeasure: 精准模式");
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST: //wrap_content
                Log.i(TAG, "onMeasure: 最大值模式");
                height = 100;
                break;
            case MeasureSpec.UNSPECIFIED: //不限制view的最大值
                Log.i(TAG, "onMeasure: 自由模式");
                break;
        }

        //设置测量尺寸
        setMeasuredDimension(width,height);

    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setTextSize(size);
        paint.setColor(color);
        canvas.drawColor(Color.BLUE);
        canvas.drawText(string,0,0,paint);


    }
}
